/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import Interface.FormRelatorios;
import com.lowagie.text.pdf.PdfWriter;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 * Classe para geraçao de relatorios iRport
 * @author Jonas Gualberto Diel
 */
public class Relatorio {
    
    private static Connection con;
    private String file;
    private Parameters[] parameters;    
        
    public Relatorio(String report_path, Connection conection, Parameters[] parametros)
    {
        this.file = report_path;
        con = conection;
        this.parameters = parametros;
    }
    
    public void gerar()
    {
        try {
                //Compila o relatorio
                JasperReport relatorio  = JasperCompileManager.compileReport(file);
                
                // Mapeia os campos de parametros para o relatorio,  mesmo que nao existam
                Map parametros = new HashMap();
                for(int i=0; i<parameters.length; i++)
                {
                    parametros.put(parameters[i].var, parameters[i].value);
                }
                
                //Executa o relatório
                JasperPrint impressao = JasperFillManager.fillReport(relatorio, parametros, con);
                
                
                // Exibe o resultado em video
                JasperViewer.viewReport(impressao, false);
                                
            } catch (JRException ex) {
                
                Logger.getLogger(FormRelatorios.class.getName()).log(Level.SEVERE, null, ex);
            }
    }   
}